﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Security.AccessControl;
using System.Security.Permissions;

namespace SmartPlant.WinCore;

/// <summary>
/// 注册表读取写入。
/// </summary>
public static class WinRegister
{
    /// <summary>
    /// spi的注册表默认地址
    /// </summary>
    public const string regPath = "SOFTWARE\\WOW6432Node\\Intergraph\\INTOOLS";

    /// <summary>
    /// 向注册表写入字符串值，并对其进行base64加密
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <param name="val">键值</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool SetStringValueKeyToEncodingBase64(string subkey, string key, string val)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, true);
            if (rk == null)
            {
                rk = Registry.LocalMachine.CreateSubKey(subkey);
                if (rk == null)
                {
                    return false;
                }
            }
            byte[] buff = System.Text.Encoding.GetEncoding("gb2312").GetBytes(val);
            val = Convert.ToBase64String(buff, 0, buff.Length);
            rk.SetValue(key, val);
            rk.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 向注册表写入字符串值，并对其进行base64加密
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <param name="val">键值</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool SetStringValueKeyToEncodingMD5(string subkey, string key, string val)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, true);
            if (rk == null)
            {
                rk = Registry.LocalMachine.CreateSubKey(subkey);
                if (rk == null)
                {
                    return false;
                }
            }

            val = MD5Encryption.Encrypt(val);
            rk.SetValue(key, val);
            rk.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 向注册表写入字符串值，不进行base64加密
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <param name="val">键值</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool SetStringValueKey(string subkey, string key, string val)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, true);
            if (rk == null)
            {
                rk = Registry.LocalMachine.CreateSubKey(subkey);
                if (rk == null)
                {
                    return false;
                }
            }
            rk.SetValue(key, val);
            rk.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 向注册表写入整型值
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <param name="val">键值</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool SetIntValueKey(string subkey, string key, int val)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, true);
            if (rk == null)
            {
                rk = Registry.LocalMachine.CreateSubKey(subkey);
                if (rk == null)
                {
                    return false;
                }
            }
            rk.SetValue(key, val);
            rk.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 从注册表读取字串值，并对值进行base64解密返回
    /// </summary>
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <param name="val">返回取得的字串值</param>
    /// <returns>返回是否成功bool值</returns> 
    public static bool GetStringValueKeyFromEncodingBase64(string subkey, string key, ref string val, string def)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, true);
            if (rk != null)
            {
                val = (string)rk.GetValue(key, "");
                byte[] buff = Convert.FromBase64String(val);
                val = System.Text.Encoding.GetEncoding("gb2312").GetString(buff, 0, buff.Length);
                if (val == "")
                    val = def;
                rk.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 从注册表读取未经加密的字串值
    /// </summary>
    /// <param name="subkey">字键</param>
    /// <param name="key">键名</param>
    /// <param name="val">返回的字串值</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool GetStringValueKey(string subkey, string key, ref string val, string def)
    {
        try
        {
            if (subkey.IsNullOrEmpty())
                return false;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, RegistryRights.ReadKey);
            if (rk != null)
            {
                val = (string)rk.GetValue(key, "");
                if (val == "")
                    val = def;
                rk.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 从注册表读取整型值
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <param name="val">返回取得的整型值，默认值为0</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool GetIntValueKey(string subkey, string key, ref int val, int def)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;

            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, RegistryRights.ReadKey);
            if (rk != null)
            {
                val = (int)rk.GetValue(key, -1);
                if (val == -1)
                    val = def;
                rk.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 取得一个注册表子键下所有键名的数组
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="valList">存放返回的数组</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool GetSubKeyValueNames(string subkey, ref IList<string> valList)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;

            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, RegistryRights.ReadKey);
            if (rk != null)
            {
                string[] val = rk.GetValueNames();
                for (int i = 0; i < val.Length; i++)
                {
                    valList.Add(val[i]);
                }
                rk.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 删除一个指定的键
    /// </summary>
    /// <param name="subkey">子键</param>
    /// <param name="key">键名</param>
    /// <returns>返回是否成功bool值</returns>
    public static bool DelValueKey(string subkey, string key)
    {
        try
        {
            if (subkey.IsNullOrEmpty()) return false;

            RegistryKey rk = Registry.LocalMachine.OpenSubKey(subkey, true);
            if (rk != null)
            {
                rk.DeleteValue(key);
                rk.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
}
